package com.cs4261.smartalarm.backend;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

/**
 * @author Joshua
 *
 */
public class AlarmsDatabaseHelper {
	private SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	private String[] columnNames = {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_ACTIVE, 
	MySQLiteHelper.COLUMN_HOUR, MySQLiteHelper.COLUMN_MINUTE, MySQLiteHelper.COLUMN_REPEAT, MySQLiteHelper.COLUMN_SUNDAY, MySQLiteHelper.COLUMN_MONDAY, 
	MySQLiteHelper.COLUMN_TUESDAY, MySQLiteHelper.COLUMN_WEDNESDAY,	MySQLiteHelper.COLUMN_THURSDAY,	MySQLiteHelper.COLUMN_FRIDAY,
	MySQLiteHelper.COLUMN_SATURDAY,	MySQLiteHelper.COLUMN_WEATHER,	MySQLiteHelper.COLUMN_W1, MySQLiteHelper.COLUMN_W2, 
	MySQLiteHelper.COLUMN_W3, MySQLiteHelper.COLUMN_W4,	MySQLiteHelper.COLUMN_TRAFFIC,	MySQLiteHelper.COLUMN_LOCATION1, 
	MySQLiteHelper.COLUMN_LOCATION2, MySQLiteHelper.COLUMN_SNOOZE, MySQLiteHelper.COLUMN_TONE};

	/**
	 * @param context
	 */
	public AlarmsDatabaseHelper(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	/**
	 * @throws SQLException
	 */
	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();
	}

	/**
	 * 
	 */
	public void close() {
		dbHelper.close();
	}

	/**
	 * @param name
	 * @return
	 */
	public long createAlarm(Alarm alarm) {
		
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_NAME, alarm.getName());
		values.put(MySQLiteHelper.COLUMN_ACTIVE, alarm.getActive());
		values.put(MySQLiteHelper.COLUMN_HOUR, alarm.getHour());
		values.put(MySQLiteHelper.COLUMN_MINUTE, alarm.getMinute());
		values.put(MySQLiteHelper.COLUMN_REPEAT, alarm.getRepeat());
		values.put(MySQLiteHelper.COLUMN_SUNDAY, alarm.getSunday());
		values.put(MySQLiteHelper.COLUMN_MONDAY, alarm.getMonday());
		values.put(MySQLiteHelper.COLUMN_TUESDAY, alarm.getTuesday());
		values.put(MySQLiteHelper.COLUMN_WEDNESDAY, alarm.getWednesday());
		values.put(MySQLiteHelper.COLUMN_THURSDAY, alarm.getThursday());
		values.put(MySQLiteHelper.COLUMN_FRIDAY, alarm.getFriday());
		values.put(MySQLiteHelper.COLUMN_SATURDAY, alarm.getSaturday());
		values.put(MySQLiteHelper.COLUMN_WEATHER, alarm.getWeatherActive()); //Keep?
		values.put(MySQLiteHelper.COLUMN_W1, alarm.getW1());
		values.put(MySQLiteHelper.COLUMN_W2, alarm.getW2());
		values.put(MySQLiteHelper.COLUMN_W3, alarm.getW3());
		values.put(MySQLiteHelper.COLUMN_W4, alarm.getW4());
		values.put(MySQLiteHelper.COLUMN_TRAFFIC, alarm.getTrafficActive()); //Keep?
		values.put(MySQLiteHelper.COLUMN_LOCATION1, alarm.getLocation1());
		values.put(MySQLiteHelper.COLUMN_LOCATION2, alarm.getLocation2());
		values.put(MySQLiteHelper.COLUMN_SNOOZE, alarm.getSnooze());
		values.put(MySQLiteHelper.COLUMN_TONE, alarm.getTone());
		
		
		long id = db.insert(MySQLiteHelper.TABLE_ALARMS, null, values);
		Cursor cursor = db.query(MySQLiteHelper.TABLE_ALARMS, columnNames,
				MySQLiteHelper.COLUMN_ID + " = " + id, null, null, null,
				null);
		cursor.moveToFirst();
		id = cursor.getLong(0);
		cursor.close();
		return id;
	}

	/**
	 * @param id
	 * @return
	 */
	public Alarm getAlarmById(long id) {
		Cursor cursor = db.query(MySQLiteHelper.TABLE_ALARMS, columnNames,
				MySQLiteHelper.COLUMN_ID + " = " + id, null, null, null,
				null);
		cursor.moveToFirst();
		Alarm newAlarm = cursorToAlarm(cursor);
		cursor.close();
		return newAlarm;
	}

	/**
	 * @param alarm
	 */
	public void updateAlarm(Alarm alarm) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_NAME, alarm.getName());
		values.put(MySQLiteHelper.COLUMN_ACTIVE, alarm.getActive());
		values.put(MySQLiteHelper.COLUMN_HOUR, alarm.getHour());
		values.put(MySQLiteHelper.COLUMN_MINUTE, alarm.getMinute());
		values.put(MySQLiteHelper.COLUMN_REPEAT, alarm.getRepeat());
		values.put(MySQLiteHelper.COLUMN_SUNDAY, alarm.getSunday());
		values.put(MySQLiteHelper.COLUMN_MONDAY, alarm.getMonday());
		values.put(MySQLiteHelper.COLUMN_TUESDAY, alarm.getTuesday());
		values.put(MySQLiteHelper.COLUMN_WEDNESDAY, alarm.getWednesday());
		values.put(MySQLiteHelper.COLUMN_THURSDAY, alarm.getThursday());
		values.put(MySQLiteHelper.COLUMN_FRIDAY, alarm.getFriday());
		values.put(MySQLiteHelper.COLUMN_SATURDAY, alarm.getSaturday());
		values.put(MySQLiteHelper.COLUMN_WEATHER, alarm.getWeatherActive()); //Keep?
		values.put(MySQLiteHelper.COLUMN_W1, alarm.getW1());
		values.put(MySQLiteHelper.COLUMN_W2, alarm.getW2());
		values.put(MySQLiteHelper.COLUMN_W3, alarm.getW3());
		values.put(MySQLiteHelper.COLUMN_W4, alarm.getW4());
		values.put(MySQLiteHelper.COLUMN_TRAFFIC, alarm.getTrafficActive()); //Keep?
		values.put(MySQLiteHelper.COLUMN_LOCATION1, alarm.getLocation1());
		values.put(MySQLiteHelper.COLUMN_LOCATION2, alarm.getLocation2());
		values.put(MySQLiteHelper.COLUMN_SNOOZE, alarm.getSnooze());
		values.put(MySQLiteHelper.COLUMN_TONE, alarm.getTone());
		
		db.update(MySQLiteHelper.TABLE_ALARMS, values, MySQLiteHelper.COLUMN_ID + " = " + alarm.getId(), null);
	}

	/**
	 * @param alarm
	 */
	public void deleteAlarm(Alarm alarm) {
	    long id = alarm.getId();
	    System.out.println("Comment deleted with id: " + id);
	    db.delete(MySQLiteHelper.TABLE_ALARMS, MySQLiteHelper.COLUMN_ID + " = " + id, null);
	}
	
	/**
	 * @param id
	 */
	public void deleteAlarm(long id) {
	    //long id = alarm.getId();
	    System.out.println("Comment deleted with id: " + id);
	    db.delete(MySQLiteHelper.TABLE_ALARMS, MySQLiteHelper.COLUMN_ID + " = " + id, null);
	}

	/**
	 * @return
	 */
	public List<Alarm> getAllAlarms() {
		List<Alarm> alarms = new ArrayList<Alarm>();
		Cursor cursor = db.query(MySQLiteHelper.TABLE_ALARMS, columnNames, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Alarm alarm = cursorToAlarm(cursor);
			alarms.add(alarm);
			cursor.moveToNext();
		}
		cursor.close();
		return alarms;
	}

	/**
	 * @param cursor
	 * @return
	 */
	private Alarm cursorToAlarm(Cursor cursor) {
		Alarm alarm = new Alarm(cursor.getLong(0), cursor.getString(1));
		alarm.setActive(cursor.getInt(2));
		alarm.setHour(cursor.getInt(3));
		alarm.setMinute(cursor.getInt(4));
		alarm.setRepeat(cursor.getInt(5));
		alarm.setSunday(cursor.getInt(6));
		alarm.setMonday(cursor.getInt(7));
		alarm.setTuesday(cursor.getInt(8));
		alarm.setWednesday(cursor.getInt(9));
		alarm.setThursday(cursor.getInt(10));
		alarm.setFriday(cursor.getInt(11));
		alarm.setSaturday(cursor.getInt(12));
		alarm.setWeatherActive(cursor.getInt(13));
		alarm.setW1(cursor.getInt(14));
		alarm.setW2(cursor.getInt(15));
		alarm.setW3(cursor.getInt(16));
		alarm.setW4(cursor.getInt(17));
		alarm.setTrafficActive(cursor.getInt(18));
		alarm.setLocation1(cursor.getLong(19));
		alarm.setLocation2(cursor.getLong(20));
		alarm.setSnooze(cursor.getInt(21));
		alarm.setTone(cursor.getString(22));
				
		return alarm;
	}
}